package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.bizToBaseData.service.Biz2BaseVehicleConfigDubboService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.VehicleConfigConstraint;
import com.yunxi.otd.pojo.VehicleConfigConstraintVO;
import com.yunxi.otd.pojo.VehicleConfigImportVO;
import com.yunxi.otd.service.VehicleConfigConstraintDubboService;
import com.yunxi.service.localService.otd.VehicleConfigConstraintService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 整车配置约束规则 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-08-05
 */
public class VehicleConfigConstraintDubboServiceImpl implements VehicleConfigConstraintDubboService {

    @Autowired
    @Qualifier(value = "vehicleConfigConstraintServiceImpl")
    VehicleConfigConstraintService vehicleConfigConstraintService;

    @Autowired
    Biz2BaseVehicleConfigDubboService biz2baseVehicleConfigDubboService;


    @Override
    public VehicleConfigConstraintVO getById(String id) {
        VehicleConfigConstraint vehicleConfigConstraint = vehicleConfigConstraintService.getById(id);
        VehicleConfigConstraintVO vehicleConfigConstraintVO = new VehicleConfigConstraintVO();
        BeanUtils.copyProperties(vehicleConfigConstraint, vehicleConfigConstraintVO);
        return vehicleConfigConstraintVO;
    }

    @Override
    public ApiResult saveOrUpdate(VehicleConfigConstraintVO vehicleConfigConstraintVO) {
        VehicleConfigConstraint vehicleConfigConstraint = new VehicleConfigConstraint();
        BeanUtils.copyProperties(vehicleConfigConstraintVO, vehicleConfigConstraint);

        LambdaQueryWrapper<VehicleConfigConstraint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleConfigConstraint::getPremise, vehicleConfigConstraintVO.getPremise())
            .eq(VehicleConfigConstraint::getOperator, vehicleConfigConstraintVO.getOperator())
            .eq(VehicleConfigConstraint::getConclusion, vehicleConfigConstraintVO.getConclusion());
        List<VehicleConfigConstraint> list = vehicleConfigConstraintService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            return ResponseUtil.fail(-1, "已存在相同的约束关系");
        }
        vehicleConfigConstraintService.saveOrUpdate(vehicleConfigConstraint);
        return ResponseUtil.ok();
    }

    @Override
    public void removeByIds(List<String> ids) {
        vehicleConfigConstraintService.removeByIds(ids);
    }

    @Override
    public PageInfo findPageByVO(VehicleConfigConstraintVO vehicleConfigConstraintVO,
        Integer pageNum, Integer pageSize) {
        return vehicleConfigConstraintService.pageByVo(vehicleConfigConstraintVO, pageNum, pageSize);
    }

    @Override
    public List listByVo(VehicleConfigConstraintVO vehicleConfigConstraintVO) {
        return vehicleConfigConstraintService.listByVo(vehicleConfigConstraintVO);
    }


    @Override
    public ApiResult getEngineeringConfig(String vehicleCode, String inColor, String outColor, String packages) {
        ////// 约束条件
        LambdaQueryWrapper<VehicleConfigConstraint> constraintsWrapper = new LambdaQueryWrapper<>();
        constraintsWrapper.eq(VehicleConfigConstraint::getState, "1");
        List<VehicleConfigConstraint> constraintsDB = vehicleConfigConstraintService.list(constraintsWrapper);
        if (CollectionUtils.isEmpty(constraintsDB)) {
            return ResponseUtil.fail(-1, "未维护约束关系");
        }

        ////// 全量工程配置
        // 全量工程配置 MAP,<key:特征值,value:特征族>
        Map<String, String> allConfigMap = biz2baseVehicleConfigDubboService.loadAllEngineeringConfig();

        // 单车默认配置MAP,<key:特征族,value:特征值>
        Map<String, String> configMap = biz2baseVehicleConfigDubboService
            .loadEngineeringConfigByVehicleCode(vehicleCode, inColor, outColor, packages, allConfigMap);


        List<VehicleConfigConstraint> constraints = new ArrayList<>(constraintsDB);
        /////// 根据约束关系调整默认工程配置 ///////
        vehicleConfigConstraintService.replaceConfig(constraints, configMap, allConfigMap);

        List<VehicleConfigImportVO> resultList = new ArrayList<>();
        configMap.forEach((familyCode, featureCode) -> {
//            System.out.println(s + "\t" + s2);
            VehicleConfigImportVO importVO = new VehicleConfigImportVO();
            importVO.setFamilyCode(familyCode);
            importVO.setFeatureCode(featureCode);
            resultList.add(importVO);
        });

        return ResponseUtil.ok(resultList);
    }

    /**
     * 查询与选装包A互斥的其他选装包
     *
     * @param packages 选装包A
     * @return 互斥的其他选装包
     */
    @Override
    public ApiResult findExcludeCustomPackCode(List<String> packages) {
        return vehicleConfigConstraintService.findExcludeCustomPackCode(packages);
    }

    /**
     * 根据 featureCode 查询必选选装包
     *
     * @param featureCode （颜色Code）
     * @return 必选选装包
     */
    @Override
    public Map<String, List<Object>> findRequiredPackagesByFeatureCode(List<String> featureCode, String vehicleCode) {
        return vehicleConfigConstraintService.findRequiredPackagesByFeatureCode(featureCode, vehicleCode);
    }
}
